//
//  LGTTypeSelectVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/16.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTTaskTypeSelectVC: LGTBaseVC {

    var type_list:Array<PostNameModel> = Array.init() //类型列表
    var buttonArray:Array<LGTButton> = []
    var currentIndex:Int = 0
    var typeSelectBlock:ClickIndexBlock? //类型选择回掉
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.setBavBarStyle(navBarHiden: true, title: "", back: false)
        
        self.view.backgroundColor = UIColor.clear
        let mainView:UIView = UIView.init()
        mainView.backgroundColor = BlackAlphaColor
        self.view.addSubview(mainView)
        mainView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.top.equalTo(NavBarH() + 45.0)
        }
        
        let count:Int = type_list.count
        let rowCount:Int = 4
        var row:Int = count/rowCount
        if count % rowCount != 0{
            row += 1
        }
        
        let y = 6
        let x = 12
        let itemH = 35
        let space = 5
        let itemW = (Int(KScreenW) - (rowCount - 1) * space - 2 * x) / rowCount
        let typeViewH = itemH * row + y * 2
        
        let typeView:UIView = UIView.init()
        typeView.backgroundColor = UIColor.white
        mainView.addSubview(typeView)
        typeView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(typeViewH)
        }
        
        for i in 0..<self.type_list.count{
            let model = self.type_list[i]
            let button:LGTButton = LGTViewCreateUtil.share.createTextLGTButton(title: model.name ?? "", font: KFont(size: 15), color: TextColor, selectedColor: YellowColor)
            typeView.addSubview(button)
            self.buttonArray.append(button)
            button.tag = i
            button.snp.makeConstraints { (make) in
                make.left.equalTo(x + (itemW + space) * (i % rowCount))
                make.width.equalTo(itemW)
                make.height.equalTo(itemH)
                make.top.equalTo(y + itemH * (i / rowCount))
            }
            
            if i == self.currentIndex{
                button.isSelected = true
            }
            button.addTarget(self, action: #selector(typeChoseAction), for: UIControl.Event.touchUpInside)
        }
        //self.typeChoseAction(sender: self.buttonArray[self.currentType])
        
        let resetBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "重置", color: TextColor, font: KFont(size: 15.0), backColor: UIColor.white)
        mainView.addSubview(resetBtn)
        resetBtn.tag = 0
        resetBtn.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
        resetBtn.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.top.equalTo(typeView.snp_bottomMargin)
            make.height.equalTo(40)
            make.width.equalTo(KScreenW / 2.0)
        }
        
        let sureBtn:UIButton = LGTViewCreateUtil.share.createButtonTitleAndImage(imageName: "", title: "确定", color: UIColor.white, font: KFont(size: 15.0), backColor: YellowColor)
        mainView.addSubview(sureBtn)
        sureBtn.tag = 1
        sureBtn.addTarget(self, action: #selector(clickAction), for: UIControl.Event.touchUpInside)
        sureBtn.snp.makeConstraints { (make) in
            make.right.equalTo(0)
            make.top.equalTo(typeView.snp_bottomMargin)
            make.height.equalTo(40)
            make.width.equalTo(KScreenW / 2.0)
        }
    }
    
    @objc func clickAction(sender:UIButton){
        
        //重置
        if sender.tag == 0{
            self.typeSelectBlock!(-1)
        }else{
            self.typeSelectBlock!(self.currentIndex)
        }
        self.dismiss(animated: false, completion: nil)
        
    }
    
    @objc func typeChoseAction(sender:UIButton){
        let index:Int = sender.tag
        self.currentIndex = index
        for btn in self.buttonArray{
            btn.isSelected = false
            if btn.tag == index{
                btn.isSelected = true
                print("true")
            }
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss(animated: false, completion: nil)
    }
}
